/**
 * jims
 */
package com.jims.master.modules.physical.service;

import com.jims.master.common.persistence.Page;
import com.jims.master.common.service.CrudService;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.modules.physical.ResultVo;
import com.jims.master.modules.physical.dao.TInformationDao;
import com.jims.master.modules.physical.dao.TResultGroupDao;
import com.jims.master.modules.physical.dao.TResultItemDao;
import com.jims.master.modules.physical.dao.TResultOfficeDao;
import com.jims.master.modules.physical.entity.TInformation;
import com.jims.master.modules.physical.entity.TResultGroup;
import com.jims.master.modules.physical.entity.TResultItem;
import com.jims.master.modules.physical.entity.TResultOffice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Service
 * @author zp
 * @version 2017-06-21
 */
@Service
@Transactional(readOnly = true)
public class TResultItemService extends CrudService<TResultItemDao, TResultItem> {
	@Autowired
	private TResultGroupDao tResultGroupDao;
	@Autowired
	private TResultOfficeDao tResultOfficeDao;
	@Autowired
	private TInformationDao tInformationDao;
	@Autowired
	private TResultItemDao tResultItemDao;

	public TResultItem get(String id) {
		return super.get(id);
	}

	/**
	 * 根据条件查询项目和项目明细列表
	 * @param tResultItem
	 * @return
	 * @author CTQ
	 */
	public List<ResultVo> findGroupAndItemList(TResultItem tResultItem) {
		return dao.findGroupAndItemList(tResultItem);
	}
	
	public Page<TResultItem> findPage(Page<TResultItem> page, TResultItem tResultItem) {
		return super.findPage(page, tResultItem);
	}
	
	@Transactional(readOnly = false)
	public int save(TResultItem tResultItem) {
		int num = 0;
		List<TResultItem> list = tResultItem.gettResultItemList();
		TResultOffice tResultOffice =list.get(0).gettResultOffice();
		if (StringUtils.isNotBlank(tResultOffice.getId())){
			tResultOffice.preUpdate();
			tResultOffice.updateOrgId();
			tResultOfficeDao.update(tResultOffice);
		}else{
			tResultOffice.preInsert();
			tResultOffice.updateOrgId();
			tResultOfficeDao.insert(tResultOffice);

		}
		TInformation tInformation = new TInformation();
		tInformation.setClinicId(list.get(0).getClinicId());
		tInformation.setBingzhong(tResultOffice.getXiaojie());
		tInformation.setJiankangjianyi(tResultOffice.getJiankangjianyi());
		tInformation.setJianyi(tResultOffice.getJianyi());
		tInformation.setWenxintishi(tResultOffice.getWenxintishi());
		tInformation.setOrgId(tResultOffice.getOrgId());
		tInformationDao.updateXj(tInformation);

		for(int i=0;i<list.size();i++){
			TResultItem tResultItem1 = list.get(i);
			String groupId = tResultItem1.getGroupId();
			if(!"".equals(tResultItem1.getItemResult()) && tResultItem1.getItemResult() != null){
			TResultGroup tResultGroup = new TResultGroup();
			tResultGroup.setId(groupId);
			tResultGroup.setIsOk("1");
			tResultGroup.setClinicId(tResultItem1.getClinicId());
			tResultGroup.preUpdate();
			tResultGroupDao.update(tResultGroup);
				if(tResultItem1.getIsNewRecord()){
					tResultItem1.preInsert();
					tResultItem1.updateOrgId();
					num += tResultItemDao.insert(tResultItem1);
				} else {
					tResultItem1.preUpdate();
					tResultItem1.updateOrgId();
					num += tResultItemDao.update(tResultItem1);
				}
			}else {
				num+=1;
			}

		}
		return num;
	}
	
	@Transactional(readOnly = false)
	public int delete(TResultItem tResultItem) {
		return super.delete(tResultItem);
	}

	/**
	 * 保存审核时数据
	 * @param resultVo
	 * @return
	 * @author CTQ
	 */
	@Transactional(readOnly = false)
	public int saveResultInfo(ResultVo resultVo) {
		int num = 0;
		if (resultVo!=null){
			/**1修改t_information主表中的is_ok=1（已审核）**/
			TInformation information = new TInformation();
			information.setClinicId(resultVo.getClinicId());
			information.setIsOk(resultVo.getStatus());
			information.setJianyi(resultVo.getJianyi());
			String itemResults = "";
			List<TResultItem> list = resultVo.getItemList();
			for(int i=0;i<list.size();i++){
				itemResults+= list.get(i).getItemResult();
			}
			information.setBingzhong(itemResults);
			tInformationDao.updateByClinicId(information);
			/**2修改t_result_item数据**/
			if (resultVo.getItemList()!=null&&resultVo.getItemList().size()>0){
				for (TResultItem item : resultVo.getItemList()){
					if(StringUtils.isNotBlank(item.getId())){
						item.updateOrgId();
						num +=dao.update(item);
					}
				}
			}
		}

		return num;
	}
	/**
	 * 取消审核
	 * @param resultVo
	 * @return
	 * @author CTQ
	 */
	@Transactional(readOnly = false)
	public int cancelAudit(ResultVo resultVo) {
		int num = 0;
		/**1修改t_information主表中的is_ok=0（取消审核）**/
		TInformation information = new TInformation();
		information.setClinicId(resultVo.getClinicId());
		information.setIsOk(resultVo.getStatus());
		num = tInformationDao.updateByClinicId(information);

		return num;
	}
	/**
	 * 总检打印
	 * @param resultVo
	 * @return
	 * @author CTQ
	 */
	public String print(ResultVo resultVo){
		String printPath = "";

		return printPath;
	}

}